# encoding: utf-8

module Capevaluation
  module Service
    class PlayerScore < CaiQiu::Core::ServiceBase
      class << self
        # 获取数据库连接
        def get_conn
          unless defined? @conn
            @conn = CaiQiu::Core::ServiceConn.DB_CAPEVALUATION
          end
          @conn
        end

        def get_record_by_player(params)
          rows = Capevaluation::Dao::PlayerScore.where(:type => params['type'], :player => params['player'])
          res_list = []
          rows.each do |item|
            res_list.push item.to_hash
          end
          res_list
        end

        def get_ordered_record
          sql = "select * from player_score order by score desc"
          rows = self.get_conn[sql]
          res_list = []
          rows.each do |item|
            res_list.push item.to_hash
          end
          res_list
        end

        def get_record_by_type(params)
          page_count = 0
          current_page = params[:page].to_i
          count = params[:count].to_i
          limit_begin = count*current_page
          #limit_end = count*(current_page+1)-1
          sql = "select * from player_score #{params[:sql_message]} order by ranking LIMIT #{limit_begin},#{count}"
          rows = self.get_conn[sql]
          res_list = []
          rows.each do |item|
            res_list.push item.to_hash
          end

          sql_page = "select * from player_score #{params[:sql_message]} order by ranking"
          rows_page = self.get_conn[sql_page]
          rows_page.each do |item|
            page_count += 1
          end
          total_page = page_count/count
          [res_list,total_page]
        end

        def update_player_score(type,player,player_total,player_success,player_fail,player_ratio,player_score,player_ranking)
          sql = "update player_score set total='#{player_total}',success='#{player_success}',fail='#{player_fail}',ratio='#{player_ratio}',score='#{player_score}',ranking='#{player_ranking}' where type='#{type}' and player = '#{player}'"
          self.get_conn[sql].update
        end

        def add_player_score(type,player,player_total,player_success,player_fail,player_ratio,player_score,player_ranking)
          info = Capevaluation::Dao::PlayerScore.new
          info[:type]  = type
          info[:player] = player
          info[:total] = player_total
          info[:success] = player_success
          info[:fail] = player_fail
          info[:ratio] = player_ratio
          info[:score] = player_score
          info[:ranking] = player_ranking
          info.save
        end

        def update_ranking(type,player,ranking)
          sql = "update player_score set ranking='#{ranking}' where type='#{type}' and player = '#{player}'"
          self.get_conn[sql].update
        end

        def is_exists(player)
          info = Capevaluation::Dao::Submits.where(:player=>player).first
          info.nil?? false : true
        end



        # def add_example(value, status)
        #   example          = Capevaluation::Dao::Example.new
        #   example[:value]  = value
        #   example[:status] = status
        #   example.save
        # end
        #
        # def get_example_by_value(value)
        #   example_list = []
        #   rows         = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                      .where(:value => value)
        #   rows.each do |row|
        #     example_list.push row.to_hash
        #   end
        #   example_list
        # end
        #
        # def get_example_by_id(id)
        #   example_list = []
        #   rows         = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                      .where(:id => id)
        #   rows.each do |row|
        #     example_list.push row.to_hash
        #   end
        #   example_list
        # end
        #
        # def list_examples
        #   example_list = []
        #   rows         = Capevaluation::Dao::Example.select(:id, :value, :status)
        #   rows.each do |row|
        #     example_list.push row.to_hash
        #   end
        #   example_list
        # end
        #
        # def mod_example(example_hash)
        #   example_model = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                       .where(:id => example_hash[:id]).first
        #   example_hash.each do |k, v|
        #     if example_model.columns.include? k
        #       if v != example_model[v]
        #         example_model[k] = v
        #       end
        #     end
        #   end
        #   example_model.save_changes
        # end
        #
        # def mod_examples(example_list)
        #   example_list.each do |example|
        #     mod_example(example)
        #   end
        # end
        #
        # def mod_value_by_status(example_hash)
        #   # 开启事务
        #   example_model = Capevaluation::Dao::Example.select(:id, :value, :status)
        #                       .where(:id => example_hash[:id]).first
        #   example_model.lock!
        #   if example_model[:status] == 0
        #     example_model[:value] = 'lock'
        #     example_model.save_changes
        #   end
        # end

      end
    end
  end
end

